﻿namespace MX.Core.Caching.Redis
{
	using MX.Core.Caching;
	using Memory;
	using MX.Core.Extensions;
	using System;
	using System.Collections.Generic;
	using System.Configuration;

	public class RedisCachingFactory : ICachingFactory
	{
		private Dictionary<string, ICaching> _cacheInstances = new Dictionary<string, ICaching> ();

		/// <summary>
		/// 获取缓存实例
		/// </summary>
		/// <returns></returns>
		public ICaching Default {
			get {
				return RedisCaching.Default;
			}
		}

		public ICaching CreateCaching (string name)
		{
			ICaching caching;
			if (!_cacheInstances.TryGetValue (name, out caching)) {
				lock (_cacheInstances) {
					if (!_cacheInstances.TryGetValue (name, out caching)) {
						caching = new RedisCaching (name);
						_cacheInstances.Add (name, caching);
					}
				}
			}
			return caching;
		}



		public RedisCaching CreateRedisCaching (string name)
		{
			ICaching caching;
			if (!_cacheInstances.TryGetValue (name, out caching)) {
				lock (_cacheInstances) {
					if (!_cacheInstances.TryGetValue (name, out caching)) {
						caching = new RedisCaching (name);
						_cacheInstances.Add (name, caching);
					}
				}
			}
			return caching as RedisCaching;
		}

		public RedisCaching CreateRedisCaching (object cacheConfigName)
		{
			throw new NotImplementedException ();
		}
	}
}

